package models

import "time"
import "gorm.io/gorm"
import "go_part/utils"
//import "fmt"

type CompanyBasic struct{
	gorm.Model
	Name string
	CompanyID string
	PassWord string
	LocalPassWord string
	LogoPath string
	Phone string `valid:"matches(^1[3-9]{1}\\d{9}$)"`
	Email string `valid:"email"`
	Identity string
	ClientIP string
	ClientPort string
	Salt string
	LoginTime time.Time
	HeartbeatTime time.Time
	LoginOutTime time.Time `gorm:"column:log_out_time" json:"log_out_time"`
	isLogout bool
	DeviceInfo string
 }

func (table *CompanyBasic) TableName() string {

	return "company_basic"
}

func GetCompanyList() []*CompanyBasic { 
	data := make([]*CompanyBasic,10)
	utils.DB.Find(&data)
	// for _,v : = range data {
	// 	fmt.Println(v)
	// }
	return data
}

func FindCompanyByName(name string) CompanyBasic {
	user := CompanyBasic{}
	utils.DB.Where("name = ?",name).Find(&user)
	return user
}

func FindCompanyByID(id string) CompanyBasic {
	user := CompanyBasic{}
	utils.DB.Where("company_id = ?",id).First(&user)
	return user
}

func FindCompanyByIDAndPassword(id string, password string) CompanyBasic {
	user := CompanyBasic{}
	utils.DB.Where("company_id = ? and pass_word = ?",id,password).First(&user)
	return user
}

func CreateCompany(user CompanyBasic) *gorm.DB {
	
	return utils.DB.Create(&user)
}

func DeleteCompany(user CompanyBasic) *gorm.DB {
	
	return utils.DB.Delete(&user)
}

func UpdateCompany(user CompanyBasic) *gorm.DB {
	
	return utils.DB.Model(&user).Updates(CompanyBasic{Name: user.Name, LogoPath: user.LogoPath, DeviceInfo: user.DeviceInfo})
}